﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.DAL;
using System.Data.SqlClient;
using System.Data;

namespace EHTSaleManager.Code.BLL
{
    public class NewsProvider
    {
        public static void IUNews(News theNews, bool IsAdd)
        {            
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUNews";
            command.CommandType = CommandType.StoredProcedure;            
            command.Parameters.Add(new SqlParameter("@NewsID",theNews.NewsID ));
            command.Parameters.Add(new SqlParameter("@TitleNews",theNews.TitleNews ));
            command.Parameters.Add(new SqlParameter("@ContentNews", theNews.ContentNews));
            command.Parameters.Add(new SqlParameter("@TypeNews",theNews.TitleNews ));
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);
        }
        public static void DeleteNews(Int32 NewsID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteNewsByNewsID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@NewsID", NewsID));

            DataProvider.ExecuteNoneQuery(command);
        }
        public static DataSet GetNews()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetNews";
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }

        public static News GetNewsbyID(int NewsID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetNewsByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@NewsID", NewsID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            News theNews = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theNews = new News();
                theNews.NewsID = Int32.Parse(ds.Tables[0].Rows[0]["NewsID"].ToString());
                theNews.TitleNews = ds.Tables[0].Rows[0]["TitleNews"].ToString();
                theNews.ContentNews = ds.Tables[0].Rows[0]["ContentNews"].ToString();
                theNews.TypeNews = ds.Tables[0].Rows[0]["TypeNews"].ToString();                
            }

            return theNews;
        }

        public static DataSet GetDetailsNewsByNewsID(Int32 NewsID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetNewsByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@NewsID", NewsID));
            return DataProvider.ExecuteDataSet(command);
        }

        public static DataSet GetNewsWithPaging(Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetNewsWithPaging";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }

        public static DataSet GetTopNews()
        {             
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTopNews";
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }


        // Lấy tim tức theo lloại tin

        //public static DataSet GetNewsByTypeNewsWithPaging(Int32 pageIndex, Int32 pageSize, out Int32 totalRecord, string TypeNews)
        //{
        //    SqlCommand command = new SqlCommand();
        //    command.CommandText = "proc_GetNewsTypeNewsByWithPaging";
        //    command.CommandType = CommandType.StoredProcedure;
        //    command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
        //    command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
        //    command.Parameters.Add(new SqlParameter("@TypeNews", TypeNews));
        //    DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
        //    return ds;
        //}
        
    }
}